<?php

/**
 * LEAGUEMan
 *
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_VALID_MOS') or die( 'Direct Access to this location is not allowed.' );

$path = $mainframe->getCfg("absolute_path");

set_include_path(
    get_include_path()
    . PATH_SEPARATOR . $path . "/components/com_leagueman/lib/LEAGUEMAN"
    . PATH_SEPARATOR . $path . "/components/com_leagueman/lib/PHPTHUMB"
);

$language_file = $path . '/components/com_leagueman/languages/' . $mainframe->getCfg("lang") . '.php';

if (file_exists($language_file)) {
    include_once $language_file;
} else {
    include_once $path . '/components/com_leagueman/languages/english.php';
}

include_once "Tableconfig.php";
include_once "Roundresult.php";
include_once "Teamresult.php";
include_once "Leaguetable.php";
include_once "Projectmanager.php";
include_once "ResultTransferObject.php";
include_once 'MatchEventTempResult.php';

function getCountry($code)
{
    global $countries;

    if (isset($countries[$code])) {
        return $countries[$code];
    } else {
        return _UNKNOWN_;
    }
}

function getCountryFlag($code)
{
    global $countries;

    if (isset($countries[$code])) {
        return "<img src=\"images/stories/leagueman/flags/$code.gif\" border=\"0\" alt=\"{$countries[$code]}\" />";
    } else {
        return "&nbsp;";
    }
}

function getCountries()
{
    global $countries;

    $list = array(mosHTML::makeOption("", _SELECT_COUNTRY_));
    foreach ($countries AS $key => $value) {
        $list[] = mosHTML::makeOption($key, $value);
    }

    return $list;
}

function __autoload($className)
{
    if(strpos($className, "LM") !== false) {
        $fileName = str_replace('_', '/', $className) . '.php';
        require $fileName;
    }
}

function isAdmin()
{
    global $my;
    return in_array($my->usertype, array('Super Administrator', 'Administrator'));
}

function isDate($date)
{
    if (ereg( "([0-9]{4})-([0-9]{2})-([0-9]{2})", $date, $regs)) {
        return checkdate($regs[2], $regs[3], $regs[1]);
    }
}

function isTime($time)
{
    if (ereg("([0-9]{2}):([0-9]{2})", $time, $regs)) {

        $hours = intval($regs[1]);
        $minutes = intval($regs[2]);

        if ($hours < 0 || $hours > 23) {
            return false;
        }

        if ($minutes < 0 || $minutes > 59) {
            return false;
        }

        return true;
    }

    return false;
}

function saveImage($path, $field, $fprefix)
{
    $upload = isset($_FILES[$field]) ? $_FILES[$field] : null;

    if($upload && is_array($upload) && !empty($upload['name'])) {

        $format = strtolower(substr($upload['name'], -3 ));
        $allowable = array ('gif', 'jpg', 'png', 'bmp');

        $noMatch = 0;
        foreach( $allowable as $ext ) {
            if ( strcasecmp( $format, $ext ) == 0 ) {
                $noMatch = 1;
            }
        }

        if(!$noMatch) {
            return false;
        }

        $filename = $fprefix . ".{$format}";

        if (!move_uploaded_file($upload['tmp_name'], $path . $filename)) {
            return false;
        } else {
            mosChmod($path . $filename);
            return $filename;
        }
    }

    return false;
}

function label($label)
{
    echo '<b>' . $label . '</b>';
}